<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学校" prop="school">
        <el-input
          v-model="queryParams.school"
          placeholder="请输入学校"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="作品" prop="workName">
        <el-input
          v-model="queryParams.workName"
          placeholder="请输入作品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="参赛人" prop="participantName">
        <el-input
          v-model="queryParams.participantName"
          placeholder="请输入参赛姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['activity:work:export']"
        >导出数据
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="workList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="学校" align="center" prop="school"/>
      <el-table-column label="报名组别" align="center" prop="groupCategory"/>
      <el-table-column label="作品名称" align="center" prop="workName"/>
      <el-table-column label="参赛姓名" align="center" prop="participantName"/>
      <el-table-column label="联系方式" align="center" prop="contactInfo"/>
      <el-table-column label="实时平均分" align="center" prop="realTimeAvgScore"/>
      <el-table-column label="提交时间" align="center" prop="commitTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.commitTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="viewWork(scope.row)"
            v-hasPermi="['activity:work:edit']"
          >查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改作品信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学校" prop="school">
          <el-input v-model="form.school" placeholder="请输入学校"/>
        </el-form-item>
        <el-form-item label="报名组别" prop="groupCategory">
          <el-input v-model="form.groupCategory" placeholder="请输入报名组别"/>
        </el-form-item>
        <el-form-item label="作品名称" prop="workName">
          <el-input v-model="form.workName" placeholder="请输入作品名称"/>
        </el-form-item>
        <el-form-item label="参赛者id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入参赛者id"/>
        </el-form-item>
        <el-form-item label="参赛姓名" prop="participantName">
          <el-input v-model="form.participantName" placeholder="请输入参赛姓名"/>
        </el-form-item>
        <el-form-item label="所属课程" prop="belongsToCourse">
          <el-input v-model="form.belongsToCourse" placeholder="请输入所属课程"/>
        </el-form-item>
        <el-form-item label="联系方式" prop="contactInfo">
          <el-input v-model="form.contactInfo" placeholder="请输入联系方式"/>
        </el-form-item>
        <el-form-item label="实时平均分" prop="realTimeAvgScore">
          <el-input v-model="form.realTimeAvgScore" placeholder="请输入实时平均分"/>
        </el-form-item>
        <el-form-item label="提交时间" prop="commitTime">
          <el-date-picker clearable
                          v-model="form.commitTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择提交时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="浏览次数" prop="snakeCase">
          <el-input v-model="form.snakeCase" placeholder="请输入浏览次数"/>
        </el-form-item>
        <el-form-item label="课堂识录" prop="classroomRecording">
          <el-input v-model="form.classroomRecording" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="教案" prop="lessonPlan">
          <el-input v-model="form.lessonPlan" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="教学实施报告" prop="teachingPracticeReport">
          <el-input v-model="form.teachingPracticeReport" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="专业人才培养方案" prop="majorTalentDevelopmentPlan">
          <el-input v-model="form.majorTalentDevelopmentPlan" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="课程标准" prop="courseStandard">
          <el-input v-model="form.courseStandard" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="其他附件" prop="otherAttachments">
          <el-input v-model="form.otherAttachments" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="创建时间" prop="createdTime">
          <el-date-picker clearable
                          v-model="form.createdTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新时间" prop="updatedTime">
          <el-date-picker clearable
                          v-model="form.updatedTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择更新时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listWork, getWork, delWork, addWork, updateWork} from "@/api/activity/work";
import {parseTime} from "../../../utils/ruoyi";

export default {
  name: "Work",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 作品信息表格数据
      workList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        school: null,
        groupCategory: null,
        workName: null,
        userId: null,
        participantName: null,
        belongsToCourse: null,
        contactInfo: null,
        realTimeAvgScore: null,
        commitTime: null,
        snakeCase: null,
        classroomRecording: null,
        lessonPlan: null,
        teachingPracticeReport: null,
        majorTalentDevelopmentPlan: null,
        courseStandard: null,
        otherAttachments: null,
        createdTime: null,
        updatedTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        school: [
          {required: true, message: "学校不能为空", trigger: "blur"}
        ],
        groupCategory: [
          {required: true, message: "报名组别不能为空", trigger: "blur"}
        ],
        workName: [
          {required: true, message: "作品名称不能为空", trigger: "blur"}
        ],
        userId: [
          {required: true, message: "参赛者id不能为空", trigger: "blur"}
        ],
        participantName: [
          {required: true, message: "参赛姓名不能为空", trigger: "blur"}
        ],
        belongsToCourse: [
          {required: true, message: "所属课程不能为空", trigger: "blur"}
        ],
        contactInfo: [
          {required: true, message: "联系方式不能为空", trigger: "blur"}
        ],
        commitTime: [
          {required: true, message: "提交时间不能为空", trigger: "blur"}
        ],
        snakeCase: [
          {required: true, message: "浏览次数不能为空", trigger: "blur"}
        ],
        createdTime: [
          {required: true, message: "创建时间不能为空", trigger: "blur"}
        ],
        updatedTime: [
          {required: true, message: "更新时间不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    parseTime,
    /** 查询作品信息列表 */
    getList() {
      this.loading = true;
      listWork(this.queryParams).then(response => {
        this.workList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        school: null,
        groupCategory: null,
        workName: null,
        userId: null,
        participantName: null,
        belongsToCourse: null,
        contactInfo: null,
        realTimeAvgScore: null,
        commitTime: null,
        snakeCase: null,
        classroomRecording: null,
        lessonPlan: null,
        teachingPracticeReport: null,
        majorTalentDevelopmentPlan: null,
        courseStandard: null,
        otherAttachments: null,
        createdTime: null,
        updatedTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加作品信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWork(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改作品信息";
      });
    },
    /**
     * 查看
     * @param row
     */
    viewWork(row) {
      const id = row.id;
      getWork(id).then(response => {
        console.log(response.data);
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWork(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWork(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除作品信息编号为"' + ids + '"的数据项？').then(function () {
        return delWork(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('activity/work/export', {
        ...this.queryParams
      }, `work_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
